#import "SEGShowadvertManager.h"

#if __has_include(<Commom/SEGModel.h>)
#import <Commom/SEGModel.h>
#else
#import "SEGModel.h"
#endif
#if __has_include(<Commom/SEGHTTPBusinessManager.h>)
#import <Commom/SEGHTTPBusinessManager.h>
#else
#import "SEGHTTPBusinessManager.h"
#endif

static id _showAdvertManager;
static dispatch_once_t onceToken;

@implementation SEGShowadvertManager
#pragma mark - SINGLE----BEGIN----
+(instancetype)sharedManager{
    dispatch_once(&onceToken, ^{
        _showAdvertManager = [[self alloc] init];
    });
    return _showAdvertManager;
}

+(void)destroyResource{
    onceToken = 0;
    _showAdvertManager = nil;
}

#pragma mark - SINGLE----END----

#pragma mark - HTTP----BEGIN----
-(void)requestShowadvertWithCode:(NSString*)code finished:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))finished{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:code forKey:@"code"];
    
    // Value -> nil or "", Do not launch tencent ad when app soft boot.
    [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"TencentLaunchAdIntervalTimeKey"];
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/cms-api/isShowAdvert",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params completed:^(BOOL isSuccessed, id result, NSString *codeType, NSError *error) {
        if (!isSuccessed) {
            finished(isSuccessed,nil,codeType,error.localizedDescription);
            return;
        }
        
        self.showAdvert = [SEGShowadvertModel yy_modelWithDictionary:result];
        
        if (self.showAdvert.data.intervalTime &&
            [self.showAdvert.data.intervalTime isKindOfClass:[NSString class]] &&
            [self.showAdvert.data.intervalTime length] > 0) {
            NSString *secondString = [@([self.showAdvert.data.intervalTime integerValue] * 60) stringValue];
            [[NSUserDefaults standardUserDefaults] setValue:secondString forKey:@"TencentLaunchAdIntervalTimeKey"];
        }
        
        finished(isSuccessed,result,codeType,error.localizedDescription);
        return;
    }];
}
#pragma mark - HTTP----END----

#pragma mark - GETTER----BEGIN----
-(SEGShowadvertModel *)showAdvert{
    if (!_showAdvert) {
        _showAdvert = [[SEGShowadvertModel alloc]init];
    }
    return _showAdvert;
}
#pragma mark - GETTER----END----
@end



